// pages/ticket/ticket.js
const app = getApp()
var t = require('./../../utils/request.js');
var u = require('./../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    actions: [
    ],
    goods_name: '',
    code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    t.toPost('/minip/ticket/info', {
      qid: u.getPageInfo().options.qid
    }, (res) => {
      if (res.data.is_all == 0) {
        this.setData({
          actions: [
          {
            name: 'A5'
          },{
            name: 'A5-W'
          },{
            name: 'A5-N'
          }]
        })
      } else {
        t.toPost('/minip/seller/deviceList', {}, (res) => {
          var arr = []
          for (var i = 0; i < res.data.length; i++) {
            arr.push({
              name: res.data[i].device
            })
          }
          this.setData({
            actions: arr
          })
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onSelect: function (event) {
    console.log(event.detail)

    this.setData({
      goods_name: event.detail,
      show: false
    })
  },

  selectDevice: function () {
    this.setData({
      show: true
    })
  },

  checkTicket: function () {
    var reg = new RegExp("^[0-9]*$")

    if (!reg.test(this.data.code)){
      wx.showToast({
        icon: 'none',
        title: '核销码必须是数字',
      })
      return;
    }

    if (!this.data.goods_name.name) {
      wx.showToast({
        icon: 'none',
        title: '请选择商品',
      })
      return ;
    }

    if (!this.data.code) {
      wx.showToast({
        icon: 'none',
        title: '请输入码编',
      })
      return;
    }
    

    t.toPost('/minip/seller/checkTicket', {
      uid: u.getPageInfo().options.uid,
      oid: u.getPageInfo().options.oid,
      qid: u.getPageInfo().options.qid,
      sid: wx.getStorageSync('sid'),
      goods: this.data.goods_name.name,
      code: this.data.code
    }, (res) => {
      console.log(res)
      if (res.code === '000000') {
        wx.showToast({
          title: '核销成功',
        })
        wx.redirectTo({
          url: '/pages/list/list',
        })
      }
    })
  },

  inputCode: function (e) {
    this.setData({
      code: e.detail
    })
  }
})